package com.example.lenovo.mywechat.homepage.mail_list;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lenovo.mywechat.R;
import com.example.lenovo.mywechat.temp.chat_page.ChatPageActivity;
import com.google.protobuf.ByteString;

import java.util.List;

public class FriendAdapter extends RecyclerView.Adapter {
    List<Friend> friendList;
    Activity mActivity;

    public FriendAdapter (List<Friend> friendList, Activity mActivity){
        this.friendList = friendList;
        this.mActivity = mActivity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //此方法是将一条会话布局返回。
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mail_list_content_item,
                viewGroup, false);
        return new RecyclerView.ViewHolder(view){};//RecyclerView.ViewHolder是一个抽象类，这里使用匿名类。
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        //此方法是为了显示什么数据、如何显示布局

        ImageView iv_icon = viewHolder.itemView.findViewById(R.id.iv_icon);
        TextView tv_name = viewHolder.itemView.findViewById(R.id.tv_name);
        RelativeLayout relativeLayout = viewHolder.itemView.findViewById(R.id.rl_friend);

        ByteString byteString = friendList.get(i).avatar;
        iv_icon.setImageDrawable(Drawable.createFromStream(byteString.newInput(), null));

        tv_name.setText(friendList.get(i).name);

        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, ChatPageActivity.class);
                intent.putExtra("group_id", friendList.get(i).group_id);
                intent.putExtra("id", friendList.get(i).id);
                intent.putExtra("avatar_type", friendList.get(i).avatar_type);
                intent.putExtra("avatar", friendList.get(i).avatar.toByteArray());
                mActivity.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return friendList.size();
    }
}
